/**********************************************************************************
 ***
 ***    Screencasting Teaching System C++ Library
 ***    Copyright (C) 2011  Jiri Novak <jiri.novak@petriny.net>
 ***                        Wadi Jalil Maluf <wadijm@gmail.com>
 ***
 ***    This file is part of the Screencasting Teaching System C++ Library.
 ***
 ***    This library is free software; you can redistribute it and/or modify
 ***    it under the terms of the GNU General Public License as published by
 ***   the Free Software Foundation; either version 3 of the License, or
 ***    (at your option) any later version.
 ***
 ***    This library is distributed in the hope that it will be useful,
 ***    but WITHOUT ANY WARRANTY; without even the implied warranty of
 ***    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ***   GNU General Public License for more details.
 ***
 ***    You should have received a copy of the GNU General Public License
 ***    along with this library; if not, write to the Free Software
 ***    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ***
 *********************************************************************************/

#include <log4cxx/logger.h>
#include <log4cxx/xml/domconfigurator.h>
#include <cstdlib>
#include <string>
#include "clientlistener.h"
#include "../common/resources.h"
#include "../client/serverservice.h"

#define ERROR 1

// Define static logger variable
log4cxx::LoggerPtr loggerServerStsMain(log4cxx::Logger::getLogger("main"));

int main(int argc, char** argv) {
	// Load configuration file
	log4cxx::xml::DOMConfigurator::configure(SERVER_STS_LOG4CXX_FILE);

	if (argc != 3) {
		LOG4CXX_FATAL(loggerServerStsMain, "wrong number of arguments specified");
		return ERROR;
	}

	int port = atoi(argv[1]);
	std::string confFolder(argv[2]);
	ClientListener s(port, confFolder);

	LOG4CXX_INFO(loggerServerStsMain, "starting server at port " << port);
	if (!s.init()) {
		LOG4CXX_FATAL(loggerServerStsMain, "server could not be started");
		return ERROR;
	}

	s.start();
	LOG4CXX_INFO(loggerServerStsMain, "server started successfully");

	char read = 0;
	while (read != 'q') {
		std::cin >> read;
	}

	LOG4CXX_INFO(loggerServerStsMain, "stopping server");
	s.close();
	s.join();

	LOG4CXX_INFO(loggerServerStsMain, "server stopped successfully! Thank you for using it!");
	return 0;
}
